package scuff.msg.json

import scuff.msg.util.Classifier

abstract class ReflectiveDispatchingJsonReader[CB, PL <: scuff.DoubleDispatch[CB]]
    extends DispatchingCharSeqJsonReader[CB]
    with Classifier[String, PL] {

  protected def deserialize(jsonPayload: String, msgType: Class[_ <: PL]): PL
  
  protected def materialize(id: String, payload: Option[String]): PL = {
    val msgType = classify(id)
    payload match {
      case Some(pl) ⇒ deserialize(pl, msgType)
      case None ⇒ msgType.newInstance
    }
  }

}